package server.game.players.packets;

import server.Config;
import server.Connection;
import server.Server;
import server.game.players.Client;
import server.game.players.PacketType;
import server.game.players.Player;
import server.game.players.PlayerHandler;
import server.game.players.PlayerSave;
import core.util.Misc;

public class Commands implements PacketType {
	public boolean resetAnim = false;

	@Override
	public void processPacket(Client c, int packetType, int packetSize) {
		String playerCommand = c.getInStream().readString();
		if (Config.SERVER_DEBUG) {
			Misc.println(c.playerName + " playerCommand: " + playerCommand);
		}
		if (c.playerRights >= 0) {
			if (playerCommand.equalsIgnoreCase("headicon")) {
				final int id = Integer.parseInt(playerCommand.substring(9));

				c.headIcon = id;
				c.getPA().requestUpdates();
			}
			if (playerCommand.equalsIgnoreCase("fixme")) {
				if (System.currentTimeMillis() - c.lastHome < 5000) {
					c.sendMessage("You must wait "
							+ (System.currentTimeMillis() - c.fixMe)
							+ "MS to use this command.");
					return;
				}
				c.getPA().movePlayer(3222, 3218, 0);
				c.lastHome = 5000;
			}
			if (playerCommand.equalsIgnoreCase("players")) {
				c.sendMessage("Current amount of players online: @red@"
						+ (PlayerHandler.getPlayerCount()) + "@bla@.");
			}
			if (playerCommand.equalsIgnoreCase("timeplayed")) {
				c.sendMessage("Time played: @red@" + c.getPlaytime() + ".");
			}
			if (playerCommand.startsWith("toggyell")) {
				final int toggle = Integer
						.parseInt(playerCommand.substring(11));
				c.toggleYell = toggle == 0 ? c.ON : c.OFF;
				final String status = toggle == 0 ? "ON" : "@red@OFF";
				c.sendMessage("You toggled your Yell " + status + ".");
			}
			if (playerCommand.startsWith("changepass")) {
				final String pazz = playerCommand.substring(11);
				if (pazz.contains(" ")) {
					c.sendMessage("Passwords cannot contains a space.");
				} else {
					c.sendMessage("Your new password has been set.");
					c.playerPass = pazz;
					PlayerSave.saveGame(c);
				}
			}
			if (c.playerRights == 3 || c.playerName.equalsIgnoreCase("ali")) {
				if (playerCommand.startsWith("teletome")) {
					if (c.inWild())
						return;
					try {
						String playerToBan = playerCommand.substring(9);
						for (int i = 0; i < Config.MAX_PLAYERS; i++) {
							if (Server.playerHandler.players[i] != null) {
								if (Server.playerHandler.players[i].playerName
										.equalsIgnoreCase(playerToBan)) {
									Client c2 = (Client) Server.playerHandler.players[i];
									c2.teleportToX = c.absX;
									c2.teleportToY = c.absY;
									c2.heightLevel = c.heightLevel;
									c.sendMessage("You have teleported "
											+ Misc.capitalize(c2.playerName)
											+ " to you.");
									c2.sendMessage("You have been teleported to "
											+ Misc.capitalize(c.playerName)
											+ "");
								}
							}
						}
					} catch (Exception e) {
						c.sendMessage("Player is probably offline.");
					}
				}
			}

			if (playerCommand.startsWith("yell")) {
				if (c.yellTimer > 0) {
					c.sendMessage("You must wait a few seconds before you can yell again.");
					return;
				}
				final String msg = playerCommand.substring(5);
				final String BAD_WORDS[] = { "bitch", "bitch", "b itch",
						"kunt", "cunt", "faggot", "fag", "fagg0t", "f4ggot",
						"f4gg0t", "f4g", "penis", "p3n15", "p3nis", "p3n1s",
						"p3ni5", "fuck", "fawk", "cock", "kawk", "cawk",
						"dick", "pussy", "vagina", ".com", ".org", ".net",
						".cqm", "c o m", "n3t", "bullshit", "ass", "a$$",
						"azz", "nigg", "nigga", "nigger", "nig", "niga" };
				for (int i = 0; i < BAD_WORDS.length; i++) {
					if (msg.contains(BAD_WORDS[i])) {
						c.sendMessage("You cannot use offensive language in the yell.");
						return;
					}
				}
				for (int j = 0; j < Server.playerHandler.players.length; j++) {
					if (Server.playerHandler.players[j] != null) {
						if (c.playerRights == 0) {
							final Client c2 = (Client) Server.playerHandler.players[j];
							if (c2.toggleYell == c2.OFF) {
								continue;
							}
							c2.sendMessage("@dbl@"
									+ Misc.capitalize(c.playerName)
									+ ": "
									+ Misc.optimizeText(playerCommand
											.substring(5)) + "");
							c.yellTimer = 10;
						}
						if (c.playerRights == 1) {
							Client c2 = (Client) Server.playerHandler.players[j];
							if (c2.toggleYell == c2.OFF) {
								continue;
							}
							c2.sendMessage("[MOD] @dbl@"
									+ Misc.capitalize(c.playerName)
									+ ": "
									+ Misc.optimizeText(playerCommand
											.substring(5)) + "");
							c.yellTimer = 10;
						}
						if (c.playerRights == 2) {
							Client c2 = (Client) Server.playerHandler.players[j];
							if (c2.toggleYell == c2.OFF) {
								continue;
							}
							c2.sendMessage("[ADMIN] @dbl@"
									+ Misc.capitalize(c.playerName)
									+ ": "
									+ Misc.optimizeText(playerCommand
											.substring(5)) + "");
							c.yellTimer = 10;
						}
						if (c.playerRights == 3) {
							Client c2 = (Client) Server.playerHandler.players[j];
							c2.sendMessage("[ADMIN] @dbl@"
									+ Misc.capitalize(c.playerName)
									+ ": "
									+ Misc.optimizeText(playerCommand
											.substring(5)) + "");
							c.yellTimer = 10;
						}
					}
				}
			}
			if (playerCommand.startsWith("noclip") && (c.playerRights != 3)) {
				return;
			}
		}
		if (c.playerRights >= 1) {
			if (playerCommand.startsWith("dupedefend")) {
				final String name = playerCommand.substring(11);
				for (final Player p : PlayerHandler.players) {
					if (p == null)
						continue;
					final Client c2 = (Client) p;
					if (c2.playerName.equalsIgnoreCase(name)) {
						c2.dupeDefender.init();
						if (c2.dupeDefender.getDuperScore() > 5) {
							c.sendMessage("@red@Duper found!");
							break;
						} else {
							c.sendMessage("@green@Account clean!");
						}
					}
				}
			}
			if (playerCommand.startsWith("mute")) {
				try {
					String playerToBan = playerCommand.substring(5);
					Connection.addNameToMuteList(playerToBan);
					for (int i = 0; i < Config.MAX_PLAYERS; i++) {
						if (Server.playerHandler.players[i] != null) {
							if (Server.playerHandler.players[i].playerName
									.equalsIgnoreCase(playerToBan)) {
								Client c2 = (Client) Server.playerHandler.players[i];
								c2.sendMessage("You have been muted by: "
										+ Misc.capitalize(c.playerName) + ".");
								break;
							}
						}
					}
				} catch (Exception e) {
					c.sendMessage("Player is probably offline.");
				}
			}
			if (c.playerRights >= 2) {
				if (playerCommand.startsWith("ipmute")) {
					try {
						String playerToBan = playerCommand.substring(7);
						for (int i = 0; i < Config.MAX_PLAYERS; i++) {
							if (Server.playerHandler.players[i] != null) {
								if (Server.playerHandler.players[i].playerName
										.equalsIgnoreCase(playerToBan)) {
									Connection
											.addIpToMuteList(Server.playerHandler.players[i].connectedFrom);
									c.sendMessage("You have IP Muted the user: "
											+ Server.playerHandler.players[i].playerName);
									Client c2 = (Client) Server.playerHandler.players[i];
									c2.sendMessage("You have been muted by: "
											+ Misc.capitalize(c.playerName));
									break;
								}
							}
						}
					} catch (Exception e) {
						c.sendMessage("Player is probably offline.");
					}
					if (playerCommand.startsWith("unipban")) {
						try {
							String playerToBan = playerCommand.substring(9);
							for (int i = 0; i < Config.MAX_PLAYERS; i++) {
								if (Server.playerHandler.players[i] != null) {
									if (Server.playerHandler.players[i].playerName
											.equalsIgnoreCase(playerToBan)) {
										Connection
												.unIPBanUser(Server.playerHandler.players[i].connectedFrom);
										c.sendMessage("You have un-IPbanned the user: "
												+ Server.playerHandler.players[i].playerName);
										break;
									}
								}
							}
						} catch (Exception e) {
							c.sendMessage("Player is probably offline.");
						}
					}
					if (playerCommand.startsWith("unipmute")) {
						try {
							String playerToBan = playerCommand.substring(9);
							for (int i = 0; i < Config.MAX_PLAYERS; i++) {
								if (Server.playerHandler.players[i] != null) {
									if (Server.playerHandler.players[i].playerName
											.equalsIgnoreCase(playerToBan)) {
										Connection
												.unIPMuteUser(Server.playerHandler.players[i].connectedFrom);
										c.sendMessage("You have un IP-muted the user: "
												+ Server.playerHandler.players[i].playerName);
										break;
									}
								}
							}
						} catch (Exception e) {
							c.sendMessage("Player is probably offline.");
						}
					}
					if (playerCommand.startsWith("unmute")) {
						try {
							String playerToBan = playerCommand.substring(7);
							Connection.unMuteUser(playerToBan);
						} catch (Exception e) {
							c.sendMessage("Player is probably offline.");
						}
					}
				}
			}
			if (c.playerRights == 3) {
				if (playerCommand.startsWith("xteleto")) {
					String name = playerCommand.substring(8);
					for (int i = 0; i < Config.MAX_PLAYERS; i++) {
						if (Server.playerHandler.players[i] != null) {
							if (Server.playerHandler.players[i].playerName
									.equalsIgnoreCase(name)) {
								c.getPA()
										.movePlayer(
												Server.playerHandler.players[i]
														.getX(),
												Server.playerHandler.players[i]
														.getY(),
												Server.playerHandler.players[i].heightLevel);
							}
						}
					}
				}
				if (playerCommand.equals("test")) {
					c.playerWalkIndex = 744;
				}
				if (playerCommand.equals("resetanim")) {
					c.playerWalkIndex = 0x333;
				}
				if (playerCommand.startsWith("ban")
						&& playerCommand.charAt(7) == ' ') { // use as ::ban
																// name
					try {
						String playerToBan = playerCommand.substring(8);
						Connection.addNameToBanList(playerToBan);
						Connection.addNameToFile(playerToBan);
						for (int i = 0; i < Config.MAX_PLAYERS; i++) {
							if (Server.playerHandler.players[i] != null) {
								if (Server.playerHandler.players[i].playerName
										.equalsIgnoreCase(playerToBan)) {
									Server.playerHandler.players[i].disconnected = true;
								}
							}
						}
					} catch (Exception e) {
						c.sendMessage("Player is not online.");
					}
				}
				if (playerCommand.startsWith("kick")) {
					try {
						String playerToKick = playerCommand.substring(5);
						for (int i = 0; i < Config.MAX_PLAYERS; i++) {
							if (Server.playerHandler.players[i] != null) {
								if (Server.playerHandler.players[i].playerName
										.equalsIgnoreCase(playerToKick)) {
									Server.playerHandler.players[i].disconnected = true;
									Server.playerHandler.players[i].properLogout = true;
								}
							}
						}
					} catch (Exception e) {
						c.sendMessage("Player is not online.");
					}
				}

				if (playerCommand.startsWith("item")) {
					try {
						String[] args = playerCommand.split(" ");
						if (args.length == 3) {
							int newItemID = Integer.parseInt(args[1]);
							int newItemAmount = Integer.parseInt(args[2]);
							if ((newItemID <= 20000) && (newItemID >= 0)) {
								c.getItems().addItem(newItemID, newItemAmount);
								c.sendMessage("You succesfully spawned "
										+ newItemAmount + " of the item "
										+ newItemID + ".");
								System.out.println("Spawned: " + newItemID
										+ " by: "
										+ Misc.capitalize(c.playerName));
							} else {
								c.sendMessage("Could not complete spawn request.");
							}
						} else {
							c.sendMessage("Use as ::item 4151 1");
						}
					} catch (Exception e) {
					}
				}
				if (playerCommand.startsWith("xteleto")) {
					String name = playerCommand.substring(8);
					for (int i = 0; i < Config.MAX_PLAYERS; i++) {
						if (Server.playerHandler.players[i] != null) {
							if (Server.playerHandler.players[i].playerName
									.equalsIgnoreCase(name)) {
								c.getPA()
										.movePlayer(
												Server.playerHandler.players[i]
														.getX(),
												Server.playerHandler.players[i]
														.getY(),
												Server.playerHandler.players[i].heightLevel);
							}
						}
					}
				}
				if (playerCommand.equalsIgnoreCase("mypos")) {
					c.sendMessage("Your position is X: " + c.absX + " Y: "
							+ c.absY);
				}
				if (playerCommand.startsWith("smsg")) {
					String[] args = playerCommand.split(" ");
					for (int j = 0; j < Server.playerHandler.players.length; j++) {
						if (Server.playerHandler.players[j] != null) {
							Client c2 = (Client) Server.playerHandler.players[j];
							c2.sendMessage(""
									+ Misc.optimizeText(playerCommand
											.substring(5)));
						}
					}
				}
				if (playerCommand.equalsIgnoreCase("random")) {
					c.sendMessage("" + Misc.random(100));
				}
				if (playerCommand.startsWith("pnpc")) {
					int npc = Integer.parseInt(playerCommand.substring(5));
					if (npc < 9999) {
						c.npcId2 = npc;
						c.isNpc = true;
						c.updateRequired = true;
						c.appearanceUpdateRequired = true;
					}
				}
				if (playerCommand.startsWith("unpc")) {
					c.isNpc = false;
					c.updateRequired = true;
					c.appearanceUpdateRequired = true;
				}
				if (playerCommand.startsWith("object")) {
					String[] args = playerCommand.split(" ");
					c.getPA().object(Integer.parseInt(args[1]), c.absX, c.absY,
							0, 10);
				}
				if (playerCommand.startsWith("empty")) {
					c.getItems().removeAllItems();
					c.sendMessage("You empty your inventory");
				}
				if (playerCommand.startsWith("tele")) {
					String[] arg = playerCommand.split(" ");
					if (arg.length > 3)
						c.getPA().movePlayer(Integer.parseInt(arg[1]),
								Integer.parseInt(arg[2]),
								Integer.parseInt(arg[3]));
					else if (arg.length == 3)
						c.getPA().movePlayer(Integer.parseInt(arg[1]),
								Integer.parseInt(arg[2]), c.heightLevel);
				}
				if (playerCommand.startsWith("switch")) {
					if (c.playerMagicBook == 0) {
						c.playerMagicBook = 1;
						c.setSidebarInterface(6, 12855);
						c.sendMessage("An ancient wisdomin fills your mind.");
						c.getPA().resetAutocast();
					} else {
						c.setSidebarInterface(6, 1151);
						c.playerMagicBook = 0;
						c.sendMessage("You feel a drain on your memory.");
						c.autocastId = -1;
						c.getPA().resetAutocast();
					}
				}
				if (playerCommand.startsWith("interface")) {
					try {
						String[] args = playerCommand.split(" ");
						int a = Integer.parseInt(args[1]);
						c.getPA().showInterface(a);
					} catch (Exception e) {
						c.sendMessage("::interface id");
					}
				}
				if (playerCommand.startsWith("npc")) {
					try {
						int newNPC = Integer.parseInt(playerCommand
								.substring(4));
						if (newNPC > 0) {
							Server.npcHandler.spawnNpc(c, newNPC, c.absX,
									c.absY, 0, 0, 120, 7, 70, 70, false, false);
						} else {
							c.sendMessage("Requested NPC does not exist.");
						}
					} catch (Exception e) {
					}
				}
				if (playerCommand.startsWith("ipban")) {
					try {
						String playerToBan = playerCommand.substring(6);
						for (int i = 0; i < Config.MAX_PLAYERS; i++) {
							if (Server.playerHandler.players[i] != null) {
								if (Server.playerHandler.players[i].playerName
										.equalsIgnoreCase(playerToBan)) {
									Connection
											.addIpToBanList(Server.playerHandler.players[i].connectedFrom);
									Connection
											.addIpToFile(Server.playerHandler.players[i].connectedFrom);
									c.sendMessage("You have IP banned the user: "
											+ Server.playerHandler.players[i].playerName
											+ " with the host: "
											+ Server.playerHandler.players[i].connectedFrom);
									Server.playerHandler.players[i].disconnected = true;
								}
							}
						}
					} catch (Exception e) {
						c.sendMessage("Player is probably offline.");
					}
				}

				if (playerCommand.startsWith("openbank")) {
					c.getPA().openUpBank();
				}
				if (playerCommand.startsWith("gfx")) {
					String[] args = playerCommand.split(" ");
					c.gfx0(Integer.parseInt(args[1]));
				}
				if (playerCommand.startsWith("update")) {
					String[] args = playerCommand.split(" ");
					int a = Integer.parseInt(args[1]);
					PlayerHandler.updateSeconds = a;
					PlayerHandler.updateAnnounced = false;
					PlayerHandler.updateRunning = true;
					PlayerHandler.updateStartTime = System.currentTimeMillis();
				}
				if (playerCommand.startsWith("ban")
						&& playerCommand.charAt(3) == ' ') {
					try {
						String playerToBan = playerCommand.substring(4);
						Connection.addNameToBanList(playerToBan);
						Connection.addNameToFile(playerToBan);
						for (int i = 0; i < Config.MAX_PLAYERS; i++) {
							if (Server.playerHandler.players[i] != null) {
								if (Server.playerHandler.players[i].playerName
										.equalsIgnoreCase(playerToBan)) {
									Server.playerHandler.players[i].disconnected = true;
								}
							}
						}
					} catch (Exception e) {
						c.sendMessage("Player is probably offline.");
					}
				}
				if (playerCommand.startsWith("unban")) {
					try {
						String playerToBan = playerCommand.substring(6);
						Connection.removeNameFromBanList(playerToBan);
						c.sendMessage(playerToBan + " has been unbanned.");
					} catch (Exception e) {
						c.sendMessage("Player is probably offline.");
					}
				}
				if (playerCommand.startsWith("anim")) {
					String[] args = playerCommand.split(" ");
					c.startAnimation(Integer.parseInt(args[1]));
					c.getPA().requestUpdates();
				}
				if (playerCommand.startsWith("setlevel")) {
					try {
						String[] args = playerCommand.split(" ");
						int skill = Integer.parseInt(args[1]);
						int level = Integer.parseInt(args[2]);
						if (level > 99) {
							level = 99;
						} else if (level < 0) {
							level = 1;
						}
						c.playerXP[skill] = c.getPA().getXPForLevel(level) + 5;
						c.playerLevel[skill] = c.getPA().getLevelForXP(
								c.playerXP[skill]);
						c.getPA().refreshSkill(skill);
					} catch (Exception e) {
					}
				}
			}
		}
	}
}
